if(loaded == undefined) {
    var drifting = false;
    var loaded = true;

    function driftAll() {
        $("*").each(function() {

             $obj = $(this);

             // store overflow attributes before explosion
             $obj.attr("data-orignal-css-overflow-x", $obj.css("overflow-x"));
             $obj.css("overflow-x", "visible");
             $obj.attr("data-orignal-css-overflow-y", $obj.css("overflow-y"));
             $obj.css("overflow-y", "visible");

             if($(this).children().length <= 1) {
                 offset = $obj.offset();

                 // store offset of each element
                 if($obj.attr("data-orignal-top") == undefined) { $obj.attr("data-orignal-top", offset.top); }
                 if($obj.attr("data-orignal-left") == undefined) { $obj.attr("data-orignal-left", offset.left); }

                 // use stored offset
                 $obj.offset({top:$obj.attr("data-orignal-top"), left: $obj.attr("data-orignal-left")});

                 // store CSS position before explosion
                 if($obj.attr("data-orignal-css-top") == undefined) { $obj.attr("data-orignal-css-top", $obj.css("top")); }
                 if($obj.attr("data-orignal-css-left") == undefined) { $obj.attr("data-orignal-css-left", $obj.css("left")); }

                 // mark object for explosion with class
                 $obj.addClass('chrome-extension-explode-the-web-drifter');
            }
       });

       // add a mouseover handler to every object marked for explosion
       $('.chrome-extension-explode-the-web-drifter').one("mouseover.drift", function() {
           drift($(this));
       });
    }

    // causes an object to flow around until the drifting global is false
    // then, it returns to its stored position
    function drift($obj) {
        if(drifting) {
            $obj.animate({"top": (randInt(0,1)?"+":"-") + "=" + randInt(-50,50), "left": (randInt(0,1)?"+":"-") + "=" + randInt(-50,50)}, randInt(750,1500), "linear", function() {drift($obj);});
        } else {
            if($obj.attr("data-orignal-css-top") != undefined) {
                 $obj.animate({"top": $obj.attr("data-orignal-css-top"), "left": $obj.attr("data-orignal-css-left")}, 800, "linear");
                 $obj.attr("data-orignal-css-top", undefined);
                 $obj.attr("data-orignal-css-left", undefined);
            }
        }
    }

    function goBackAll() {
       $('.chrome-extension-explode-the-web-drifter').unbind("mouseover.drift");
       setTimeout(function() {
           $("*").each(function() {
               $obj = $(this);
               $obj.css("overflow-x", $obj.attr("data-orignal-css-overflow-x"));
               $obj.attr("data-orignal-css-overflow-x", undefined);
               $obj.css("overflow-y", $obj.attr("data-orignal-css-overflow-y"));
               $obj.attr("data-orignal-css-overflow-y", undefined);
           });
       }, 1500+800);
    }

    function randInt(min, max) {
        return Math.round(Math.random()*(max - min)) + min;
    }
}

if(!drifting) {
    chrome.extension.sendRequest({driftState:"loading"});
    drifting = true;
    driftAll();
    chrome.extension.sendRequest({driftState:"drifting"});
} else {
    drifting = false;
    goBackAll();
    chrome.extension.sendRequest({driftState:"stopped"});
}

